let app = getApp()
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        headerHeight: 0,
        active: 0,
        statusList: [{
            title: '全部',
            status: ''
        }, {
            title: '待支付',
            status: 0
        }, {
            title: '待发货',
            status: 2
        }, {
            title: '已发货',
            status: 3
        }, {
            title: '已完成',
            status: 5
        }, {
            title: '售后',
            status: -1
        }],
        list: [],
        pageNum: 1,
        pageSize: 10,
        lastPage: false,
        isFirst: true
    },
    //切换状态
    change(e) {
        if (this.data.isFirst) {
            return
        }
        let index = e.detail.index
        this.setData({
            active: index,
            pageNum: 1,
            list: []
        })
        this.getOrderlist()
    },

    //跳转详情
    goDetail(e) {
        let id = e.currentTarget.dataset.id
        let isService = e.currentTarget.dataset.service
        wx.navigateTo({
            url: `/pages/Me/detail?id=${id}&isService=${isService}`,
        })
    },
       //删除订单
       delete(e) {
        let orderNumber=e.currentTarget.dataset.order
        let index=e.currentTarget.dataset.index
        let that=this
        wx.showModal({
            title: '提示',
            content: '是否删除订单?',
            success(res) {
                if (res.confirm) {
                  
                    Controller.orderdelete({orderNumber}).then(res => {
                        wx.showToast({
                            title: "删除成功",
                            icon:'none'
                        })

                     let list  =  that.data.list

                     list.splice(index,1)

                     that.setData({
                         list
                     })
 

                    
                    })
                }
            }
        })
    },

    //获取订单列表
    getOrderlist() {
        if (this.data.active != 5) {
            Controller.orderlist({
                orderStatus: this.data.statusList[this.data.active].status,
                page: this.data.pageNum,
                pageSize: this.data.pageSize,
            }).then(res => {
                let list = this.data.list
                res.data.baseData.list.forEach(item => {
                    item.num = 0
                    item.createTime = util.formatTime_2(item.createTime, '-')
                    item.itemList.forEach(data => {
                        data.specsList = data.stockSpecs ? data.stockSpecs.split(',') : []
                        item.num += data.amount
                    })
                })
                list = list.concat(res.data.baseData.list)
                this.setData({
                    list,
                    lastPage: res.data.baseData.lastPage,
                })
            })
        } else {
            Controller.refundlist({
                page: this.data.pageNum,
                pageSize: this.data.pageSize,
            }).then(res => {
                let list = this.data.list
                res.data.baseData.list.forEach(item => {
                    item.num = 0
                    item.createTime = util.formatTime_2(item.createTime, '-')
                    item.itemList.forEach(data => {
                        data.specsList = data.stockSpecs ? data.stockSpecs.split(',') : []
                        item.num += data.amount
                    })
                })
                list = list.concat(res.data.baseData.list)
                this.setData({
                    list,
                    lastPage: res.data.baseData.lastPage,
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.status) {
            let active = this.data.statusList.findIndex(item => item.status == options.status)
            this.setData({
                active
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.setData({
             headerHeight: app.globalData.headerHeight
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.setData({
            pageNum: 1,
            list: [],
        })
        setTimeout(() => {
            this.setData({
                isFirst: false
            })
        })
        this.getOrderlist()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.lastPage) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        this.getOrderlist()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})